// pages/details/details.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgs: [
      "../../image/aoudi1.jpg",
      "../../image/dazong3.jpg",
      "../../image/aoudi4.jpg",
      "../../image/dazong1.jpg",
      "../../image/jili2.jpg",
      "../../image/bieke1.jpg",
      "../../image/linmu1.jpg",
    ],
    goods: [{
        id: "1",
        goodsImage: "../../image/aoudi2.jpg",
        goodsName: "奥迪 A3 Limousine",
        goodsAddress: "广州",
        goodsPrice: "200",
        goodsPriceOld: "300",
      },
      {
        id: "2",
        goodsImage: "../../image/linmu1.jpg",
        goodsName: "铃木 利亚纳A6两厢",
        goodsAddress: "北京",
        goodsPrice: "20.8",
        goodsPriceOld: "300",
      },
      {
        id: "3",
        goodsImage: "../../image/bieke1.jpg",
        goodsName: "别克 昂科旗",
        goodsAddress: "广州",
        goodsPrice: "50.8",
        goodsPriceOld: "300",
      },
    ],
    listsdata: [],
    num: 0,
    collect : true,
  },
  goCart() {
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },

  goHome() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },
  goPay() {
    wx.navigateTo({
      url: '/pages/pay/pay',
    })
  },

  collection(){
    this.setData({
      collect : !this.data.collect,
    })
    if(!this.data.collect){
      // console.log(this.data.listsdata)
      wx.setStorageSync('carlists', this.data.listsdata)
    }
  },




  getCart() {
    let index = wx.getStorageSync('index')
    var CartLists = {
      id: this.data.goods[index].id,
      goodsName: this.data.goods[index].goodsName,
      goodsPrice: this.data.goods[index].goodsPrice,
      goodsImage: this.data.goods[index].goodsImage,
      goodsAddress: "广州",
      num: 1,
    }
    let GoodsCarList = wx.getStorageSync('GoodsCarList'); //获取数据
    if (GoodsCarList) {
      // 返回数据下标
      let index = GoodsCarList.findIndex(item => {
        return item.id == this.data.goods.id;
      })
      if (index != -1) { //添加数量
        GoodsCarList[index].num += 1;
      } else {
        // 1.2有数据没有相同数据 添加数据push
        GoodsCarList.push(CartLists);
      }
      wx.setStorageSync('GoodsCarList', GoodsCarList)
    } else {
      // 2.没有数据添加数据
      wx.setStorageSync('GoodsCarList', [CartLists])
    }
    this.setData({
      num: wx.getStorageSync('GoodsCarList').length
    })
    console.log(wx.getStorageSync('GoodsCarList').length)
  },

  getslice() {
    let index = wx.getStorageSync('index')
    this.setData({
      listsdata : {
        goodsName: this.data.goods[index].goodsName,
        goodsPrice: this.data.goods[index].goodsPrice,
        goodsPriceOld: this.data.goods[index].goodsPriceOld,
        goodsImage: this.data.goods[index].goodsImage,
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getslice();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      num: wx.getStorageSync('GoodsCarList').length || 0
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})